package net.phoenix.repository.value;

import java.util.Calendar;

import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;

import net.phoenix.repository.RepositoryRuntimeException;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class DoubleValue extends BasicValue {
	private double value;

	public DoubleValue(double value) {
		this.value = value;
	}
	public DoubleValue(String str){
		if ((str != null) && (str.length() > 0))
			this.value = Double.parseDouble(str);
	}

	@Override
	public String getString() {
		return Double.toString(this.value);
	}

	@Override
	public double getDouble() {
		return this.value;
	}

	@Override
	public long getLong() {
		return (long) value;
	}


	@Override
	public boolean getBoolean() {
		return this.value != 0;
	}

	
	@Override
	public Calendar getDate() {
		Calendar date = Calendar.getInstance();
		date.setTimeInMillis((long) this.value);
		return date;
	}

	@Override
	public int getType() {
		return PropertyType.DOUBLE;
	}

	@Override
	public int compareTo(Value o) {
		Value value = (Value) o;
		try {
			return Double.compare(this.value, value.getDouble());

		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}
	}

	@Override
	public boolean equals(Object o) {
		if (this.compareTo((Value)o) == 0)
			return true;
		else
			return false;
	}
}
